package whr.modules.assets.controller;

import java.util.Arrays;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import whr.modules.assets.entity.ReceiveEntity;
import whr.modules.assets.entity.RepairsEntity;
import whr.modules.assets.entity.ScrapEntity;
import whr.modules.assets.service.RepairsService;
import whr.common.utils.PageUtils;
import whr.common.utils.R;
import whr.modules.assets.service.ScrapService;


/**
 * 资产维修
 *
 * @author liuli
 * @email 2547121830.com
 * @date 2024-09-23 11:36:58
 */
@RestController
@RequestMapping("assets/repairs")
@Slf4j
public class RepairsController {
    @Autowired
    private RepairsService repairsService;
    @Autowired
    private ScrapService scrapService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("assets:repairs:list")
    public R list(@RequestParam Map<String, Object> params){
        log.info("parmas{}",params);
        PageUtils page = repairsService.queryByRole(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{repairsId}")
    @RequiresPermissions("assets:repairs:info")
    public R info(@PathVariable("repairsId") Long repairsId){
		RepairsEntity repairs = repairsService.getById(repairsId);

        return R.ok().put("repairs", repairs);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("assets:repairs:save")
    public R save(@RequestBody RepairsEntity repairs){
		repairsService.save(repairs);

        return R.ok();
    }

    /**
     * 把报修申请加入到报修表中
     * @param repairs
     * @return
     */
    @RequestMapping("/repairsApply")
    @RequiresPermissions("assets:receive:repair")
    public R applySave(@RequestBody RepairsEntity repairs){
        log.info("repires{}",repairs);
        repairsService.applySave(repairs);

        return R.ok();
    }
    /**
     * 报废，把数据添加在报废表中更新其他表的状态
     */
    @RequestMapping("/scrapAdd")
    @RequiresPermissions("assets:repairs:scrapAdd")
    public R scrapAdd(@RequestBody ScrapEntity scrap){
        log.info("repairs{}",scrap);
        try{
            scrapService.scrapAdd(scrap);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok("维修完成");
    }
    /**
     * 维修完成
     */
    @RequestMapping("/repairsOk")
    @RequiresPermissions("assets:repairs:repairsOk")
    public R repairsOk(@RequestBody RepairsEntity repairs){
        log.info("repairs{}",repairs);
        try{
            repairsService.updateRepairs(repairs);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok("维修完成");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("assets:repairs:update")
    public R update(@RequestBody RepairsEntity repairs){
		repairsService.updateById(repairs);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("assets:repairs:delete")
    public R delete(@RequestBody Long[] repairsIds){
		repairsService.removeByIds(Arrays.asList(repairsIds));

        return R.ok();
    }

}
